<?php
header('content-type:text/html;charset=utf-8');
$host = "localhost";
$user = "root";
$password = "";
$db = "library";


$mysqli = new mysqli($host, $user, $password, $db); //实例化mysqli对象，连接mysql数据库

//获取输入的用户名 
$id = $_POST['id'];
$did = $_POST['did'];
$password = $_POST['password'];
//向数据库查询是否存在相同的用户名
$query = "SELECT * FROM user WHERE id = '$did'"; 
$result = mysqli_query($mysqli, $query); //如果结果集中有数据，则用户名已存在 

if(mysqli_num_rows($result) > 0) {
    $pd = "SELECT * FROM user WHERE password = '$password' && id = $did"; 
    $res = mysqli_query($mysqli, $pd);
    
    if($id ==''){
        echo "<script>alert('请输入新ID');window.location.href='id.html'</script>"; 
     }
    
    else if($password == ''){
        echo "<script>alert('请输入正确的密码！');window.location.href='id.html'</script>"; 
    }
    
    else if(mysqli_num_rows($res) > 0){
        $sql = "UPDATE `user` set `id` = $id WHERE `id` = $did";
        mysqli_query($mysqli,"UPDATE user SET id=$id WHERE id=$did");
        mysqli_close($mysqli);
        echo "<script>alert('ID修改成功！');window.location.href='id.html'</script>"; 
    }
    else{
    echo "<script>alert('密码输入错误！');window.location.href='id.html'</script>"; 

    }
}
else {            
    echo "<script>alert('用户ID输入错误！');window.location.href='id.html'</script>"; 
}

?>